//
//  StationDetailModel.m
//  AirQuality
//
//  Created by Mac on 16/1/22.
//  Copyright © 2016年 Po. All rights reserved.
//

#import "StationDetailModel.h"

#define OnedayMS 3670016.000000

@interface StationDetailModel ()

@property (strong, nonatomic) NSArray * oneMon;    //一个月数据
@property (strong, nonatomic) NSArray * oneYear;   //一年数据

@end

@implementation StationDetailModel

- (instancetype)initWith:(NSString *)cityCode
{   self = [super init];
    if (self) {
        _oneMon = [NSArray array];
        _oneYear = [NSArray array];
        [self getStationOneMonthRealTimeAQIForApp:cityCode];
        [self getStation365DayAQIForApp:cityCode];
    }
    return self;
}

//构造方法
- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
//类方法
+ (instancetype)initWithDic:(NSDictionary *)dic
{
    return [[self alloc] initWithDic:dic];
}
//字典转模型
+ (NSArray *)stationDetailYear:(NSArray *)arr
{
    NSMutableArray * stationYear = [NSMutableArray array];
   
    for (NSDictionary * dic in arr) {
        StationDetailModel * stationModel = [[StationDetailModel alloc] initWithDic:dic];
        [stationYear addObject:stationModel];
    }
    return stationYear;
}


- (void)getStationOneMonthRealTimeAQIForApp:(NSString *)cityCode
{
    if (!cityCode) {
        return;
    }
    [RJNetWork myNetRequestWithAddress:[NetURL defaultURL].getStationOneMonthRealTimeAQIForApp data:@{@"stationCode":cityCode} completeBlock:^(id responseObject) {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        for(NSDictionary * dic in responseObject) {
            NSDictionary * data = dic[@"data"];
            NSDictionary * columns = data[@"columns"];
            [array addObject:columns];
        }
        _oneMon = [NSArray arrayWithArray:array];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OneMonthData" object:nil userInfo:@{@"result":@"1"}];
    } failBlack:^(id response) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"OneMonthData" object:nil userInfo:@{@"result":@"0"}];
    }];
}

- (void)getStation365DayAQIForApp:(NSString *)cityCode
{
    if (!cityCode) {
        return;
    }
    [RJNetWork myNetRequestWithAddress:[NetURL defaultURL].getStation365DayAQIForApp data:@{@"stationCode":cityCode} completeBlock:^(id responseObject) {
        _oneYear = responseObject;
//        NSError * err = nil;
//        NSArray * dic = responseObject;
//        wokan * wo = [[wokan alloc] initWithDictionary:dic error:&err];
//        NSArray * model = [nikan arrayOfModelsFromDictionaries:_oneYear error:nil];
//        nikan * ni = model[0];
//        NSLog(@"---%@",ni.AQI);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OneYearData" object:nil userInfo:@{@"result":@"1"}];
    } failBlack:^(id response) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OneYearData" object:nil userInfo:@{@"result":@"0"}];
    }];
}

- (NSDictionary *)get365dataWithType:(NSString *)type
{
    if (!_oneYear) {
        return nil;
    }
    NSString * key = type;
    NSString * Ikey = [key isEqualToString:@"AQI"]?key:[NSString stringWithFormat:@"%@%@",@"I",key];
    NSMutableArray * dataArr = [NSMutableArray array];
    NSMutableArray * timeArr = [NSMutableArray array];
    NSMutableArray * Inum = [NSMutableArray array];
    for (NSInteger i = 0; i < _oneYear.count; i ++) {
        NSDictionary * dic = _oneYear[i];
        [dataArr addObject:dic[type]];
        NSString * Istr = dic[Ikey];
        if ([Istr isKindOfClass:[NSNull class]]||[Istr isEqualToString:@"—"]) {
            [Inum addObject:@"0"];
        }else{
            [Inum addObject:dic[Ikey]];
        }
        NSString * timeStr = dic[@"TIMEPOINT"];
        NSString * time = [timeStr substringWithRange:NSMakeRange(0, 10)];
        [timeArr addObject:time];
    }
    NSString * max = [self getMax:dataArr];
    NSArray * resultArr = @[dataArr,timeArr,Inum];
    return @{type:resultArr,@"max":max};
}

#pragma mark -- 公众版24小时数据
- (NSDictionary *)get30DataWithType:(NSString *)type
{
    if (!_oneMon) {
        return nil;
    }
    NSMutableArray * array = [[NSMutableArray alloc] init];
    NSMutableArray * colNumArr = [[NSMutableArray alloc] init];
    NSMutableArray * timeArray = [[NSMutableArray alloc] init];
    NSMutableArray * timePointArr = [NSMutableArray array];
    NSMutableArray * Inum = [NSMutableArray array];
    NSString * colNum;
    NSString * key = type;
    if ([type isEqualToString:@"PM₁₀"]) {
        key = @"PM10";
    }
    colNum = [NSString stringWithFormat:@"%@",key];
    if ([key isEqualToString:@"PM2.5"]) {
        key = @"PM2_5";
    }
    if ([key isEqualToString:@"AQI"]) {
        colNum = @"AQI";
    }
     NSString * Ikey = [key isEqualToString:@"AQI"]?key:[NSString stringWithFormat:@"%@%@",@"I",key];
    for (NSInteger i = 0; i < _oneMon.count; i ++) {
        NSDictionary * dic = _oneMon[i];
        if (dic[key]) {
            [array addObject:dic[key]];
            [colNumArr addObject:dic[colNum]];
            NSString * Istr = dic[Ikey];
            if ([Istr isKindOfClass:[NSNull class]]||[Istr isEqualToString:@"—"]) {
                [Inum addObject:@"0"];
            }else{
                [Inum addObject:dic[Ikey]];
            }
        }
        if (!dic[key]) {
            [array addObject:@"0"];
            [colNumArr addObject:@"0"];
            [Inum addObject:@"0"];
        }
        if(dic[@"TIMEPOINT"]){
            NSTimeInterval time;
            if (DeviceType < 5) {
                time = [dic[@"TIMEPOINT"] floatValue] / 1000;
            }else{
                time = [dic[@"TIMEPOINT"] integerValue] / 1000;
            }
            [timePointArr addObject:dic[@"TIMEPOINT"]];
            NSString * timeString = [RJTools getTimeWithFormat:@"yyyy-MM-dd HH:mm" time:[NSDate dateWithTimeIntervalSince1970:time]];
            [timeArray addObject:timeString];
        }
        //当当天时间不存在时
        if (!dic[@"TIMEPOINT"]) {
            if (i > 0) {
                //                NSDictionary * befDic = [timePointArr lastObject];
                NSTimeInterval  todayTime = [[timePointArr lastObject] floatValue]+OnedayMS;
                [timePointArr addObject:[NSString stringWithFormat:@"%lf",todayTime]];
                NSTimeInterval todayTimeS ;
                if (DeviceType < 5) {
                    todayTimeS = todayTime / 1000;
                }else{
                    todayTimeS = todayTime/ 1000;
                }
                NSString * timeString = [RJTools getTimeWithFormat:@"yyyy-MM-dd HH:mm" time:[NSDate dateWithTimeIntervalSince1970:todayTimeS]];
                [timeArray addObject:timeString];
            }
        }
    }
    NSString * max = [self getMax:array];
    NSArray * resultArr = @[array,timeArray,Inum];
    return @{type:resultArr,@"max":max};
}

#pragma mark -- 求数组中最大值
- (NSString *)getMax:(NSArray *)arr
{
    NSComparator cmptr = ^(id obj1, id obj2){
        if ([obj1 floatValue] > [obj2 floatValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 floatValue] < [obj2 floatValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    
    NSArray *array = [arr sortedArrayUsingComparator:cmptr];
    NSString *max = [array lastObject];
    return max;
}
@end
